const express = require('express');
const router = express.Router();
const Pool = require('../pool.js');
//console.log(Pool);
//1、用户注册
router.post('/reg',(req,res,next)=>{
	//获取post请求的数据
	let obj = req.body;
	console.log(obj);
	//验证各项数据是否为空
	if(!obj.uname){
		res.json({code:401,msg:'unama不能为空'});
		//阻止往后直行
		return;
	}
	//验证密码
	if(!obj.upwd){
		res.send({code:402,msg:'upwd不能为空'});
		return;
	}
	if(!obj.email){
		res.send({code:403,msg:'email不能为空'});
		return;
	}
	if(!obj.phone){
		res.send({code:404,msg:'phone不能为空'});
		return;
	}
	//验证手机号码格式
	if( !/^1[3-9]\d{9}$/.test(obj.phone) ){

		res.send({code:405,msg:'手机号码格式错误'});
	}

	Pool.query('insert into xz_user set ?',[obj],(err,result)=>{
		if(err){
			//如果数据库执行出现了错误，交给下一个错误处理的中间件
			next(err);
			return;
		};
		console.log(result);
		res.send({code:200,msg:'注册成功'});
	});
});

//2、用户登录
router.post('/login',(req,res,next)=>{
	let obj = req.body;
	//console.log(obj);
	if(!obj.uname){
		res.send({code:401,msg:'uname不能为空'});
		return;
	}
	if(!obj.upwd){
		res.send({code:402,msg:'upwd不能为空'});
		return;
	}

	Pool.query('select * from xz_user where uname = ? and upwd = ?',[obj.uname,obj.upwd],(err,result)=>{
		if(err){
			next(err);
			return;
		};
		console.log(result);
		
		if(result[0]){
			res.send({code:200,msg:'登录成功'});
		}else{
			res.send({code:404,msg:'登录失败'});
		}
	});
});

//3、修改用户资料
//http://127.0.0.1:8080/v1/users/
router.put('/',(req,res,next)=>{
	let obj = req.body;
	let count = 400;
	//console.log(obj);
	for(let k in obj ){
		console.log(k,obj[k]);
		count++;
		if(!obj[k]){
			res.send({code: count,msg:`${k}不能为空`});
			return;
		}
	}
		
	Pool.query('update xz_user set ? where uid = ?',[obj,obj.uid],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		if(result.affectedRows){
			//console.log(result);
			res.send({code:200,msg:'修改成功'});
		}else{
			res.send({code:201,msg:'修改失败'});
		}
	});
});

//4、查询用户
router.get('/:uid',(req,res,next)=>{
	let obj = req.params;
	Pool.query('select * from xz_user where ?',[obj],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		if(result.length!==0){
			//console.log(result);
			res.send({code:200,msg:'查询成功',data:result});
		}else{
			//console.log(result);
			res.send({code:201,msg:'查询失败'});
		}
	});
});

//5、删除用户
router.delete('/:uid',(req,res,next)=>{
	let obj = req.params;
	Pool.query('delete from xz_user where ?',[obj],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		console.log(result);
		if(result.affectedRows){
			res.send({code:200,msg:'删除成功'});
		}else{
			res.send({code:201,msg:'删除失败'});
		}
	});
});

//6、用户列表(get /)
router.get('/',(req,res,next)=>{
	let obj = req.query;
	console.log(obj);
	//验证是否为空：如果为空设置默认值
	if(!obj.pno) obj.pno=1;
	if(!obj.count) obj.count = 5;
	console.log(obj);
	//计算开始查询的值
	let start = (obj.pno - 1) * obj.count;
	//每页的数据量转为数值型
	let size = parseInt(obj.count);
	//分页查询
	Pool.query('select uid,uname,email,phone from xz_user limit ?,?',[start,size],(err,result)=>{
		if(err){
			next(err);
			return;
		}
		console.log(result)
		res.send({code:200,msg:'查找成功',data:result});
	});

	
});

module.exports = router;